var articleCounter = 1;

function ArticleProvider() {
  var base = this;
  this.dummyData = [];

  this.findAll = function(callback) {
    console.log(base.dummyData);
    callback(null, base.dummyData);
  };

  this.findById = function(id, callback) {
    var result = null;
    for(var i in base.dummyData) {
      if (base.dummyData[i]._id == id) {
        result = this.dummyData[i];
        break;
      }
    }
    callback(null, result);
  };

  this.save = function(articles, callback) {
    var article = null;
    if (articles.length === undefined)
      articles = [articles];
    for(var i in articles) {
      article = articles[i];
      console.log(article[i]);
      article._id = articleCounter++;
      article.created_at = new Date();
      if(article.comments === undefined)
        article.comments = [];
      for(var j in article.comments) {
        article.comments[j].created_at = new Date();
      }
      base.dummyData.push(article);
    }
    callback(null, articles);
  };

  /* Lets bootstrap with dummy data */
  this.bootstrap = function() {
    base.save([
    {title: 'Post one', body: 'Body one', comments:[{author:'Bob', comment:'I love it'}, {author:'Dave', comment:'This is rubbish!'}]},
    {title: 'Post two', body: 'Body two'},
    {title: 'Post three', body: 'Body three'}
    ], function(error, articles){});
  }
}
exports.ArticleProvider = ArticleProvider;
